package com.iqismart.shorturl.intercept;

import com.iqismart.shorturl.config.WebSite;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description
 * @Auther sty
 * @createTime 2019-10-24 12:22
 */
public class RedirectInterceptor implements HandlerInterceptor {
    public WebSite webSite;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        final String serverName = request.getServerName();
        final int serverPort = request.getServerPort();
        webSite.url = serverName+":"+serverPort;
        if(serverName.contains(".")){
            webSite.first = serverName.replaceAll("www\\.","").split("\\.")[0];
            webSite.end = serverName.replaceAll("www\\.","").split("\\.")[1];
        }else{
            webSite.first = serverName;
            webSite.end = "";
        }

        return true;
    }
}
